#!/bin/bash
######
###Usage: ./install_gst.sh [options]
##    options:
##        install       - download source code and install gstreamer and gst plugins
##        uninstall     - uninstall gstreamer and gst plugins
##
##    e.g.
##         ./install_gst.sh install
##         ./install_gst.sh uninstall
#####
###########################################
#
#   @file: install_gst.sh
#   Author: zbc
#
###########################################
shellname=$(basename $0)

### configure gst_version and orc_version
gst_version="1.16"
orc_version="orc-0.4.28"
# gst_version="1.18"
#gst_version="master"
gst_src="${HOME}/gst"

orc="${gst_src}/orc"
gstreamer="${gst_src}/gstreamer"
gst_plugins_base="${gst_src}/gst-plugins-base"
gst_plugins_good="${gst_src}/gst-plugins-good"
gst_plugins_ugly="${gst_src}/gst-plugins-ugly"
gst_plugins_bad="${gst_src}/gst-plugins-bad"
gstreamer_vaapi="${gst_src}/gstreamer-vaapi"

rm_dir()
{
    for i in "$@"
    do
        if [ "${i}" = "" ];then
            continue
        fi
        if [ -d ${i} ];then
            rm -rf ${i}
        fi
    done
}

check_status()
{
    code=$1

    if [ "X${code}" != "X0" ];then
        echo "Error Code=${code}"
        exit $code
    fi
}

# create base path
mkdir -p ${gst_src}
check_status $?

apt_install()
{
    for i in "$@"
    do
        if [ "${i}" = "" ];then
            continue
        fi

        # skip package if installed
        # ret=$(sudo apt search ${i} |grep -E "^${i}/.*installed")
		# 适配中文版本Ubuntu
        # ret=$(sudo apt search ${i} |grep -E "^${i}/.*installed|^${i}/.*已安装")
        sudo apt search ${i} |grep -E "^${i}/.*installed|^${i}/.*已安装"
        if [ "$?" = "0" ];then
			echo "skip install $i"
            continue
        fi
        sudo apt install ${i} -y
        check_status $?
    done
}

check_version()
{
    v=$1
    if [ "X" = "X${v}" ];then
        return 0
    else
        branch_list=$(git branch)
        res=$(echo $branch_list |grep -E "^\* ${v}$")
        ret=$(echo $branch_list | grep -E "fatal|error")
        if [ "${ret}" != "" ];then
            echo $branch_list;
            return 1
        fi
        if [ "$res" = "" ];then
            git checkout $v
            check_status $?
        fi
    fi
}

## download gstreamer
download_gst()
{
    # remove directory if already exists
    rm_dir ${gstreamer} ${gst_plugins_base} ${gst_plugins_good} ${gst_plugins_ugly} ${gst_plugins_bad} ${gstreamer_vaapi} ${orc}

    #Download source code to the specified directory
    # mk_dir ${gst_src}
    cd ${gst_src}
    echo "download and uncompress ..."

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gstreamer.git
    check_status $?

    git clone https://gitlab.freedesktop.org/gstreamer/orc.git
    check_status $?

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gst-plugins-base.git
    check_status $?

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gst-plugins-good.git
    check_status $?

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gst-plugins-ugly.git
    check_status $?

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad.git
    check_status $?

    git clone -b ${gst_version} https://gitlab.freedesktop.org/gstreamer/gstreamer-vaapi.git
    check_status $?
}

# compile and install current directory source code
install_obj()
{
    if [ "$1" != "" ];then
        version=$1
    else
        version=${gst_version}
    fi

    # 当CPU总核心数n大于3时开启n-1个线程编译,否则开始n个线程
    total_core=$(grep 'core id' /proc/cpuinfo |wc -l)
	if [ $total_core -gt 3 ];then
		thread=$(expr $total_core - 1)
	else
		thread=${total_core}
	fi

    # checkout branch
    check_version ${version}

    ./autogen.sh
    check_status $?
    make -j${thread}
    check_status $?
    sudo make install
    check_status $?
}

### install gstreamer
install_gst()
{
    sudo apt install git autoconf autopoint libtool bison flex gtk-doc-tools libglib2.0-dev libssl-dev libva-dev libgtk2.0-dev
    check_status $?
    # apt_install git autoconf autopoint libtool bison flex gtk-doc-tools libglib2.0-dev libssl-dev libva-dev libgtk2.0-dev

    # download source code if directory not exist
    if [ ! -d ${gstreamer} -o ! -d ${gst_plugins_base} -o ! -d ${gst_plugins_good}  -o ! -d ${orc} \
    -o ! -d ${gst_plugins_ugly} -o ! -d ${gst_plugins_bad} -o ! -d ${gstreamer_vaapi} ];then
        download_gst
    fi

    echo "\n\n\n------------------------install gstreamer------------------------"
    cd ${gstreamer}
    install_obj

    echo "\n\n\n------------------------install orc------------------------"
    cd ${orc}
    git checkout ${orc_version}
    # install_obj ${orc_version}
    if [ ! -f ./configure ];then
        $(./autogen.sh && make distclean)
    fi
    ./configure
    check_status $?
    make -j4
    check_status $?
    sudo make install
    check_status $?

    echo "\n\n\n------------------------install gst_plugins_base------------------------"
    cd ${gst_plugins_base}
    # check and switch branch
    check_version ${gst_version}
    # install_obj
    if [ ! -f ./configure ];then
        $(./autogen.sh && make distclean)
    fi
    ./configure
    check_status $?
    make -j4
    check_status $?
    sudo make install
    check_status $?

    echo "\n\n\n------------------------install gst_plugins_good------------------------"
    cd ${gst_plugins_good}
    install_obj

    echo "\n\n\n------------------------install gst_plugins_ugly------------------------"
    cd ${gst_plugins_ugly}
    install_obj

    echo "\n\n\n------------------------install gst_plugins_bad------------------------"
    cd ${gst_plugins_bad}
    install_obj

    echo "\n\n\n------------------------install gstreamer_vaapi------------------------"
    cd ${gstreamer_vaapi}
    install_obj

}

# uninstall gstreamer
uninstall_gst()
{
    ###  uninstall gstreamer ###
    cd ${gstreamer_vaapi}
    sudo make uninstall
    make distclean

    cd ${gst_plugins_bad}
    sudo make uninstall
    make distclean

    cd ${gst_plugins_ugly}
    sudo make uninstall
    make distclean

    cd ${gst_plugins_good}
    sudo make uninstall
    make distclean

    cd ${gst_plugins_base}
    sudo make uninstall
    make distclean

    cd ${gstreamer}
    sudo make uninstall
    make distclean

}


help_msg()
{
msg="
Usage: ./${shellname} [options]

    options:
        install       - download source code and install gstreamer and gst plugins
        uninstall     - uninstall gstreamer and gst plugins

    e.g.
         ./${shellname} install
         ./${shellname} uninstall

"
    echo "${msg}" 
}

# main
if [ $# -eq 1 ];then
    exe_type=$(echo "$1" | tr 'A-Z' 'a-z' )
    if [ "${exe_type}" != "install" -a "${exe_type}" != "uninstall" ];then
        echo "[error] unknow option: $1"
        help_msg
        exit 1;
    fi

    echo "${exe_type} gstreamer[y/n]:"
    read choose;
    echo "Your Choice:${choose}"
    choose=$(echo "${choose}" | tr 'A-Z' 'a-z' )
    if [ "X${choose}" != "Xy" ];then
        echo "Cancel ${exe_type}"
        exit 1;
    fi

    sudo pwd

    if [ "Xinstall" = "X${exe_type}" ];then
        echo "install....."
        install_gst
    elif [ "Xuninstall" = "X${exe_type}"  ];then
        echo "uninstall....."
        uninstall_gst
    fi
else
    help_msg 
fi

